﻿/*
专家分类选择操作脚本
*/

//树形的配置项目
var categorySetting = {
    view: {
        selectedMulti: false
    },
    check: {
        chkboxType: { "Y": "ps", "N": "ps" },
        enable: true
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        //onClick: onClick
    }
};

var _OPERATION_SELECT_CATEGORY_urlGetCategorys = "";

//定义已经选择的专家分类ID数组
var _OPERATION_SELECT_CATEGORY_selectedCategoryIDs = new Array();
//定义已经选择的专家分类Code数组
var _OPERATION_SELECT_CATEGORY_selectedCategoryCodes = new Array();
//定义已经选择的专家分类Name数组
var _OPERATION_SELECT_CATEGORY_selectedCategoryNames = new Array();

//定义操作的对话框ID
var _OPERATION_SELECT_CATEGORY_dialogID = "";

//定义操作的树形ID
var _OPERATION_SELECT_CATEGORY_treeObjID = "";

//定义操作后，要显示已勾选记录的信息的页面元素ID
var _OPERATION_SELECT_CATEGORY_infoShowID = "";


//初始化选择专家分类，在需要弹出选择专家分类的页面装载时，初始化树形数据以及对话框
function _OPERATION_SELECT_CATEGORY_initSelectCategory() {
    //设定操作的参数
    //第一个参数代表对话框ID
    if (arguments.length >= 1) _OPERATION_SELECT_CATEGORY_dialogID = arguments[0];
    //第二个参数代表树形ID
    if (arguments.length >= 2) _OPERATION_SELECT_CATEGORY_treeObjID = arguments[1];
    //第三个参数代表显示勾选的属性节点名称的页面元素ID
    if (arguments.length >= 3) _OPERATION_SELECT_CATEGORY_infoShowID = arguments[2];

    //获取专业分类数据，初始化树形
    $.ajax({
        type: "GET",
        url: _OPERATION_SELECT_CATEGORY_urlGetCategorys,
        dataType: "json",
        success: function (data) {
            //初始化专业专家的所属分类
            $.fn.zTree.init($("#" + _OPERATION_SELECT_CATEGORY_treeObjID), categorySetting, data);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });

    //初始化对话框
    $("#" + _OPERATION_SELECT_CATEGORY_dialogID).dialog({
        hide: true,    //点击关闭是隐藏,如果不加这项,关闭弹窗后再点就会出错.
        autoOpen: false,
        height: 380,
        width: 800,
        modal: true, //蒙层（弹出会影响页面大小）
        title: "专家分类选择",
        overlay: { opacity: 0.5, background: "black", overflow: "auto" },
        buttons: {
            "确定": function () {
                //设置已选的记录
                _OPERATION_SELECT_CATEGORY_setSelectdData();
                //关闭当前Dialog
                $(this).dialog("close");
            },
            "取消": function () {
                //关闭当前Dialog
                $(this).dialog("close");
            }

        }
    });
}

//根据树形的勾选状态，设置当前的选定参数
function _OPERATION_SELECT_CATEGORY_setSelectdData() {
    //重新初始化已选的数据
    _OPERATION_SELECT_CATEGORY_selectedCategoryIDs = new Array();
    _OPERATION_SELECT_CATEGORY_selectedCategoryCodes = new Array();
    _OPERATION_SELECT_CATEGORY_selectedCategoryNames = new Array();

    var zTree = $.fn.zTree.getZTreeObj(_OPERATION_SELECT_CATEGORY_treeObjID);
    var nodes = zTree.getCheckedNodes(true);
    for (var i = 0, l = nodes.length; i < l; i++) {
        if (!nodes[i].isParent) {
            _OPERATION_SELECT_CATEGORY_selectedCategoryIDs.push(nodes[i].id);
            _OPERATION_SELECT_CATEGORY_selectedCategoryNames.push(nodes[i].name);
        }
    }

    //设置已经定义的显示当前勾选记录名称的页面元素
    var infoShow = $("#" + _OPERATION_SELECT_CATEGORY_infoShowID);
    if (infoShow != null) {
        var strC = "";
        if (_OPERATION_SELECT_CATEGORY_selectedCategoryIDs.length > 0) {
            for (var i = 0; i < _OPERATION_SELECT_CATEGORY_selectedCategoryNames.length; i++) {
                if (strC == "") {
                    strC = _OPERATION_SELECT_CATEGORY_selectedCategoryNames[i];
                } else {
                    strC += "，" + _OPERATION_SELECT_CATEGORY_selectedCategoryNames[i];
                }
            }
        } else {
            strC = "无";
        }
        infoShow.html(strC);
    }
}

//打开对话窗（strDialogID为对话框的ID），显示专家分类树形（strTreeID为树形的ID）
//当对话框返回为OK时，获取当前树形的勾选ID，填充已选专家分类ID数组，关闭对话框
//当对话框返回为Cancel时，关闭对话框
function _OPERATION_SELECT_CATEGORY_selectCategory() {
    //循环已选分类ID数组，设置树形节点的勾选项目
    var treeObj = $.fn.zTree.getZTreeObj(_OPERATION_SELECT_CATEGORY_treeObjID);
    for (var i = 0; i < _OPERATION_SELECT_CATEGORY_selectedCategoryIDs.length; i++) {
        var nodes = treeObj.getNodesByParam("id", _OPERATION_SELECT_CATEGORY_selectedCategoryIDs[i], null);
        if (nodes.length > 0) {
            treeObj.selectNode(nodes[0]);
            treeObj.expandNode(nodes[0], true, true, false);
        }
    }

    //打开对话框
    $("#" + _OPERATION_SELECT_CATEGORY_dialogID).dialog("open");
}